layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;


    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 'full-150'
         ,url:"/admin/log/list"
         ,cols: [[
             {field:'id',width:60,title: 'ID'}
             ,{field:'word',title: '搜索词',width:170}
             ,{field:'ip', title: 'IP',width:170}
             ,{field:'createTime',title: '创建时间'}
         ]]
         ,page:true
     });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'edit'){
         }else if(obj.event == 'delete'){
         }else {
             //编辑项目
            layer.msg("操作错误！");
         }
     });


    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                name:$("#name").val(),
                author:$("#author").val()
            }
        })
    });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }



    //跳转到新增页面
    $("#toAdd").on("click",function () {
        var url = "/admin/label/toAdd";
        $(this).attr("data-url",url);
        parent.addTab($(this));
    });

    $("#run").on("click",function () {
        layer.msg('爬取数据中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

        $.ajax({
            type:"POST"
            ,url:"/admin/spider/run"
            ,success:function (res) {
                layer.msg("爬取成功，请查看console");
                 console.log(res.data.result);
                table.reload("data",{
                    page:{
                        curr:1
                    }
                })
            }
            ,error:function () {
                layer.msg("服务器错误！");
            }
        });
    })



});
